package com.lab.hbase.labAPI;

import java.util.ArrayList;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
//import org.apache.hadoop.hbase.client.HConnection;
//import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
	

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HbaseTest {

	/**
	 * 配置ss
	 */
	static Configuration config = null;
	private Connection connection = null;
	private Table table = null;
//	private Admin admin = null;
	@Before
	public void init() throws Exception {
		System.out.println("##连接初始化");
		// 创建hbase配置信息，默认读取hbase-site.xml文件内容进行配置
				config = HBaseConfiguration.create();
				System.out.println("配置文件："+config);
//				修改该配置信息
//				config.set("hbase.zookeeper.quorum", "slave1,slave2,slave3");// zookeeper地址
//				config.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
				
//				获取hbase连接
				connection = ConnectionFactory.createConnection(config);
				System.out.println("连接是否关闭： "+connection.isClosed());
				
//				获取待处理的table对象
				table = connection.getTable(TableName.valueOf("t_user"));
				System.out.println("获取表："+table);
				
	}
	/**
	 * delete一个表
	 * 
	 * @throws Exception
	 */
	@Test
	@SuppressWarnings("deprecation")
	public void a1_deleteTable() throws MasterNotRunningException, ZooKeeperConnectionException, Exception {
		System.out.println("=======delete table t_user");
		// HBaseAdmin admin = new HBaseAdmin(config);
		System.out.println("a1_deleteTable");
		Admin admin = connection.getAdmin();
		TableName tn = TableName.valueOf("t_user");
		admin.disableTable(tn);
		admin.deleteTable(tn);
		admin.close();
		
		
	}
	
	/**
	 * 创建一个表
	 * 
	 * @throws Exception
	 */
	@Test
	public void a2_createTable() throws Exception {
		System.out.println("=====create table t_user");
		// 创建表管理类
		// HBaseAdmin admin = new HBaseAdmin(); // hbase表管理
		Admin admin = connection.getAdmin();
		// 创建表描述类
		TableName tableName = TableName.valueOf("t_user"); // 表名称
		HTableDescriptor desc = new HTableDescriptor(tableName);
		// 创建列族的描述类
		HColumnDescriptor family = new HColumnDescriptor("info"); // 列族
		// 将列族添加到表中
		desc.addFamily(family);
		HColumnDescriptor family2 = new HColumnDescriptor("info2"); // 列族
		// 将列族添加到表中
		desc.addFamily(family2);
		// 创建表
		admin.createTable(desc); // 创建表
		admin.close();
		
		
	}

	

	/**
	 * 向hbase中增加数据
	 * 
	 * @throws Exception
	 */
	@SuppressWarnings({ "deprecation", "resource" })
	@Test
	public void insertData() throws Exception {
		System.out.println("======insert data to table.");
//		table.setAutoFlushTo(false);
//		table.setWriteBufferSize(534534534);
		ArrayList<Put> arrayList = new ArrayList<Put>();
		for (int i = 1; i < 10; i++) {
			Put put = new Put(Bytes.toBytes("rowkey_zhang_" + i));
			
			put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan" + i));
			put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes("pw" + i));
			arrayList.add(put);
		}
		for (int i = 11; i < 20; i++) {
			Put put = new Put(Bytes.toBytes("rowkey_li_" + i));
			
			put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lisi" + i));
			put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes("pw" + i));
			arrayList.add(put);
		}
		for (int i = 21; i < 30; i++) {
			Put put = new Put(Bytes.toBytes("rowkey_wang_" + i));
			
			put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("wangwu" + i));
			put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes("pw" + i));
			arrayList.add(put);
		}

		// 插入数据
		table.put(arrayList);
		

	}

	/**
	 * 修改数据
	 * 
	 * @throws Exception
	 */
	@Test
	public void updateData() throws Exception {
		System.out.println("======update date");
		Put put = new Put(Bytes.toBytes("rowkey_zhang_1"));
		
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("new_name_zhangsan"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("password"), Bytes.toBytes("pw_new"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name222"), Bytes.toBytes("name222"));
		// 插入数据
		table.put(put);
		
	}

	/**
	 * 删除数据
	 * 
	 * @throws Exception
	 */
	@Test
	public void deleteData() throws Exception {
		System.out.println("=======delete data");
		Delete delete = new Delete(Bytes.toBytes("rowkey_2"));
		table.delete(delete);

		
	}
	/**
	 * 全表扫描
	 * 
	 * @throws Exception
	 */
	@Test
	public void scanData() throws Exception {
		System.out.println("scan data");
		Scan scan = new Scan();
		scan.setLimit(10);
		ResultScanner scanner = table.getScanner(scan);
		for (Result result : scanner) {
			System.out.print("rowkey：" + Bytes.toString(result.getRow()));
			System.out.print(" name:"+Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")))+" | ");
			System.out.println("password:"+Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));
			
		}
	}
	
	@After
	public void close() throws Exception {
		System.out.println("=====close table and connection on hbase.");
		table.close();
		connection.close();
		
		System.out.println("********************************************");
		System.out.println("********************************************");
		System.out.println("");
		System.out.println("");
	}

}